import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { useUserStore } from "@/stores/userStore";
import { ElMessage } from "element-plus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "app",
      component: HomeView,
      children: [
        {
          path: "",
          name: "home",
          component: () => import("@/views/article/ListView.vue"),
        },
        {
          path: "article",
          name: "article",
          children: [
            {
              path: "detail/:id",
              name: "articleDetail",
              component: () => import("../views/article/Detail.vue"),
            },
            {
              path: "edit/:id?",
              name: "articleEdit",
              component: () => import("../views/article/Edit.vue"),
            },
          ],
        },
        {
          path: "user",
          name: "user",
          children: [
            {
              path: "profile/:id",
              name: "userProfile",
              component: () => import("../views/user/UserProfile.vue"),
            },
          ],
        },
        {
          path: "systemNotice",
          name: "systemNotice",
          component: () => import("../views/systemNotice.vue"),
        },
        {
          path: "notice",
          name: "notice",
          component: () => import("../views/notice/notice.vue"),
          children: [
            {
              path: "comment",
              name: "comment",
              component: () => import("../views/notice/comments.vue"),
            },
            {
              path: "message",
              name: "message",
              component: () => import("../views/notice/message.vue"),
            },
            {
              path: "message/:id",
              name: "messageDetail",
              component: () => import("../views/notice/messageDetail.vue"),
            },
            {
              path: "like",
              name: "like",
              component: () => import("../views/notice/like.vue"),
            },
            {
              path: "price",
              name: "price",
              component: () => import("../views/notice/price.vue"),
            },
          ],
        },
      ],
    },

    {
      path: "/admin",
      name: "admin",
      component: () => import("@/views/admin/Home.vue"),
      redirect: "/admin/homepage",
      children: [
        {
          path: "user",
          name: "adminUser",
          component: () => import("@/views/admin/User.vue"),
        },

        {
          path: "article",
          name: "adminArticle",
          component: () => import("@/views/admin/Article.vue"),
        },
        {
          path: "articleAdudit",
          name: "articleAdudit",
          component: () => import("@/views/admin/ArticleAdudit.vue"),
        },
        {
          path: "type",
          name: "type",
          component: () => import("@/views/admin/Type.vue"),
        },
        {
          path: "notice",
          name: "adminnotice",
          component: () => import("@/views/admin/Notice.vue"),
        },
        {
          path: "homepage",
          name: "homepage",
          component: () => import("@/views/admin/HomePage.vue"),
        },
      ],
    },
    {
      path: "/adminLogin",
      name: "adminLogin",
      component: () => import("@/views/admin/Login.vue"),
    },
    {
      path: "/adminRegister",
      name: "adminRegister",
      component: () => import("@/views/admin/Register.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path != "/" && !useUserStore().isLogin && !to.path.includes("admin")) {
    ElMessage.error("请先登录");
    next({ path: "/" });
    return;
  }
  next();
});

export default router;
